using System;
using System.Collections.Generic;
using System.Text;

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Collections;
using System.Windows.Forms;

namespace Adoor.VSDesigner.CollectionEditor
{
    class LinkedDomainCollectionEditor : BaseDataCollectionEditor
    {
        public LinkedDomainCollectionEditor(Type type)
            : base(type)
        {
        }

        protected override bool CanRemoveInstance(object value)
        {
            Adoor.Object.Domain.LinkedDomainGroup table1 = (Adoor.Object.Domain.LinkedDomainGroup)value;
            if (table1.GetType().IsSubclassOf(typeof(Adoor.Object.Domain.LinkedDomainGroup)))
            {
                return false;
            }
            return true;
            //return this.DomainCollection.CanRemove(table1);
        }

        protected override object CreateInstance(Type type)
        {
            object obj1 = base.CreateInstance(typeof(Adoor.Object.Domain.LinkedDomainGroup));
            
            Adoor.Object.Domain.LinkedDomainGroup group = (Adoor.Object.Domain.LinkedDomainGroup)obj1;
            //string text1 = DataDesignUtil.GetUniqueTableName(this.DomainCollection, "Table", 1);
            //group.ObjectResolver = this.DomainGroupCollection.ObjectResolver;  
            this.DomainGroupCollection.Add(group);
            group.ObjectResolver = this.DomainGroupCollection.ObjectResolver;  
            return obj1;
        }

        protected override void DestroyInstance(object value)
        {
            Adoor.Object.Domain.LinkedDomainGroup group = (Adoor.Object.Domain.LinkedDomainGroup)value;
            group.ObjectResolver = null;
            this.DomainGroupCollection.Remove(group);
            base.DestroyInstance(group);
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            base.EditValueMask &= -33;
            object obj1 = context.Instance;
            if ((obj1 != null) && (obj1 is Adoor.Object.Domain.LinkedDomainObjectResolver))
            {
                this.resolver = (Adoor.Object.Domain.LinkedDomainObjectResolver)obj1;
            }
            if (this.resolver.GetType().IsSubclassOf(typeof(Adoor.Object.Domain.LinkedDomainObjectResolver)))
            {
                base.EditValueMask &= -129;
            }
            this.DomainGroupCollection = (Adoor.Object.Domain.LinkedDomainCollection)value;
            object obj2 = base.EditValue(context, provider, value);
            this.resolver = null;
            this.DomainGroupCollection = null;
            return obj2;
        }

        protected override void OnFormCreated()
        {
            base.DisplayedPropertyName = "LinkedDomainCollection";
        }


        protected override string FormCaption
        {
            get
            {
                return "Edit LinkedDomainCollection";
            }
        }
        protected override string HelpTopic
        {
            get
            {
                return "vs.data.collectioneditor.table";
            }
        }

        private Adoor.Object.Domain.LinkedDomainObjectResolver resolver;
        private Adoor.Object.Domain.LinkedDomainCollection DomainGroupCollection;
    }

}
